extends Button



func _ready():
	set_value(1)
	

var cur_item_info
var label_attr = "name"

func _on_select_item(index, value):
	cur_item_info = list[index]
	
	text = cur_item_info[label_attr]


var title = "选择"
var list = []
var list_data = []


func set_data(title, list, label_attr = "name"):
	self.title = title
	self.list = list
	self.label_attr = label_attr
	
	self.list_data = []
	

	for item in list:
		list_data.append(item[label_attr])
	

func set_value(id):	
	for i in range(list.size()):
		var item = list[i]
		if item["id"] == id:
			text = item[label_attr]
			cur_item_info = item
			return
	
	_on_clear_button_up()

func get_value():
	if cur_item_info:
		return cur_item_info["id"]
	else:
		return -1
		


func _on_clear_button_up():
	cur_item_info = null
	text = ""
	hint_tooltip = text



func _on_SearchSelect_button_up():
	var ui = MainTool.getUILayer().add_ui("res://tool/common/PopUp/ListSearchPopup.tscn", 0, false)
	ui.popup_centered()
	ui.set_data(title, list_data, funcref(self, "_on_select_item"), -1)
